package work.tree;

import work.TreeNode;

import java.util.*;

public class Problem199 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null) return ans;
        Queue<TreeNode> queque = new LinkedList<>();
        queque.add(root);

        while (!queque.isEmpty()) {
            int size = queque.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queque.poll();
                if (poll.left != null) queque.add(poll.left);
                if (poll.right != null) queque.add(poll.right);
                if (i == size - 1) ans.add(poll.val);
            }
        }
        return ans;
    }
}
